package org.cqut.system.resource_course.controller;

import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.resource.domain.Resource;
import org.cqut.system.resource_course.domain.ResourceCourse;
import org.cqut.system.resource_course.service.ResourceCourseService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author XiAoXu
 * @date 2021/11/12 22:00
 * @description
 */
@RestController
@RequestMapping("/resourceCourse")
public class ResourceCourseController {
    private final ResourceCourseService resourceCourseService;

    public ResourceCourseController(ResourceCourseService resourceCourseService) {
        this.resourceCourseService = resourceCourseService;
    }

    @DeleteMapping()
    public ResultInfo delete(@RequestBody ResourceCourse resourceCourse) {
        boolean flag = resourceCourseService.removeResourceOutCourse(resourceCourse);
        if (flag) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResultInfo saveResourceCourse(@RequestBody ResourceCourse resourceCourse) {
        boolean flag = resourceCourseService.save(resourceCourse);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @RequestMapping(method = RequestMethod.GET, path = "/list/{courseId}")
    public ResultInfo getResourceList(@PathVariable String courseId) {
        List<Resource> resources = resourceCourseService.selectResourceListByCourseId(courseId);
        if (Objects.nonNull(resources)) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message, resources);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }
}
